package com.vm.action.system;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Preparable;
import com.vm.action.ActionSuper;
import com.vm.dao.system.IMenu;
import com.vm.entity.Menu;
import com.vm.entity.User;
import com.vm.log4j.CustomizeLog4j;

/**
 * @description 菜单管理
 * @time 2011-08-08
 * @author osnt
 * **/
@SuppressWarnings("serial")
public class MenuAction extends ActionSuper implements Preparable{
	private static Logger _log = CustomizeLog4j.getLogger(MenuAction.class);
	private Menu menu;
	private IMenu menuDAO;
	private List<Menu> listMenu;
	private Map grpMap;
	private Map<String,String> flgMap;
	private String[] grpValues;
	public void prepare() throws Exception {
		super.setButtonGrpMap(menuDAO.seletButtonGrp((User)this.getSession().get("user"), "menuAction"));
		flgMap = new HashMap<String, String>();
		flgMap.put("1", "是");
		flgMap.put("0", "否");
	}

	public String list() throws Exception {
		listMenu = menuDAO.findByParentid("0");
		return SUCCESS;

	}

	public String add() throws Exception {
		listMenu =  menuDAO.findByParentid(menu.getParentid());
		grpMap = menuDAO.getCheckBoxMap("select prmssVal as id, name as name from GrpButton");
		return SUCCESS;
	}
	public String saveorUpdate() throws Exception {
		if(menu.getId() != null && !"".equals(menu.getId())){
			this.update();
		}else{
			this.save();
		}
		listMenu =  menuDAO.findByParentid(menu.getParentid());
		String parentid = menu.getParentid();
		menu = new Menu();
		menu.setParentid(parentid);
		grpMap = menuDAO.getCheckBoxMap("select prmssVal, name from GrpButton");
		grpValues = null;
		return SUCCESS;
	}
	public String save() throws Exception {
		String[] msgArg = new String[] { "menuAction_add.do?menu.parentid="+menu.getParentid(),"menuAction_list.do" };
		String msg = null;
		try {
			menu.setPrmssCd(menuDAO.getGrp(menu.getClass().getName(), "prmssCd"));
			menu.setButtonGrp(menuDAO.getButtonValues(grpValues));
			menuDAO.save(menu);
			msg = super.getResourceMessage("insert_menu_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("insert_menu_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;

	}

	public String edit() throws Exception {
		menu = menuDAO.findById(menu.getId());
		grpMap = menuDAO.getCheckBoxMap("select prmssVal, name from GrpButton");
		grpValues =menuDAO.getGrpValues(menu.getButtonGrp());
		if(menu.getParentid()!=null && !"0".equals(menu.getParentid())){
			listMenu =  menuDAO.findByParentid(menu.getParentid());
			return "saveOrUpdate";
		}
		return SUCCESS;
	}

	public String update() throws Exception {
		String[] msgArg = new String[] { "menuAction_list.do" };
		String msg = null;
		try {
			menu.setButtonGrp(menuDAO.getButtonValues(grpValues));
			menuDAO.update(menu);
			msg = super.getResourceMessage("update_menu_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			e.printStackTrace();
			msg = super.getResourceMessage("update_menu_err", msgArg);
		}

		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public String delete() throws Exception {
		String[] msgArg = new String[] { "menuAction_list.do" };
		String msg = null;
		try {
			menuDAO.deleteById(menu.getId());
			msg = super.getResourceMessage("delete_menu_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("delete_menu_err", msgArg);
		}

		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public void setMenuDAO(IMenu menuDAO) {
		this.menuDAO = menuDAO;
	}

	public List<Menu> getListMenu() {
		return listMenu;
	}

	public void setListMenu(List<Menu> listMenu) {
		this.listMenu = listMenu;
	}

	public IMenu getMenuDAO() {
		return menuDAO;
	}

	public Map getGrpMap() {
		return grpMap;
	}

	public void setGrpMap(Map grpMap) {
		this.grpMap = grpMap;
	}

	public String[] getGrpValues() {
		return grpValues;
	}

	public void setGrpValues(String[] grpValues) {
		this.grpValues = grpValues;
	}

	public Map<String, String> getFlgMap() {
		return flgMap;
	}

	public void setFlgMap(Map<String, String> flgMap) {
		this.flgMap = flgMap;
	}
}
